package cn.edu.zut.cs.Servlet;

import cn.edu.zut.cs.Model.Book;
import cn.edu.zut.cs.Service.BookService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "BookServlet", urlPatterns = "/BookServlet")
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book b = new Book();
        b.setIsdn(request.getParameter("isdn"));
        b.setName(request.getParameter("name"));
        b.setPrice(Float.parseFloat(request.getParameter("price")));

        BookService service = new BookService();
        try {
            service.insertBook(b);

        }catch (Exception e) {}

        HttpSession session = request.getSession(true);
        session.setAttribute("book",b);

        RequestDispatcher requestDispatcher=request.getRequestDispatcher("index.jsp");
        requestDispatcher.forward(request,response);




    }
}
